#include "Scene.h"

/*-----------------------------------------------------------------------------------------------*/
bool Scene::init(Importer* pImporter, Input* pInput)
{
   m_pInput = pInput;
   return onInit(pImporter);
}
/*-----------------------------------------------------------------------------------------------*/
void Scene::update(float deltaTime)
{
   std::map<std::string, Entity*>::iterator it;

   for ( it = m_entityMap.begin(); it != m_entityMap.end(); ++it )
   {
      it->second->update(deltaTime);
   }
     
   this->checkCollisions();

   onUpdate();
}
/*-----------------------------------------------------------------------------------------------*/
void Scene::draw(Renderer* renderer)
{
   std::map<std::string, Entity*>::iterator it;
   
   for(it = m_entityMap.begin(); it != m_entityMap.end(); ++it)
   {
      it->second->draw(renderer);
   }

   onDraw();
}
/*-----------------------------------------------------------------------------------------------*/
bool Scene::addEntity(Entity* entity)
{
//    std::map<std::string, Entity*>::iterator it;
// 
//    for(it = m_entityMap.begin(); it != m_entityMap.end(); ++it)
//    {
//       if ( it->first == entity->getName() )
//       {
//          return false;
//       }
//    }

   m_entityMap[entity->getName()] = entity;

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
void Scene::removeEntity(Entity* entity)
{
   m_entityMap.erase(entity->getName()); 
}
/*-----------------------------------------------------------------------------------------------*/
bool Scene::addToCollisionGrup( std::string& name, Entity* entity )
{
   if (name.empty())
   {
      std::cout << "failed on addToCollisionGroup";
      return false;
   }

   entity->setCollisionGroup(name);

   m_collisionMap[name].push_back(entity);
   
   return true;
}
/*-----------------------------------------------------------------------------------------------*/
bool Scene::removeFromCollisionGroup( std::string& name )
{
   std::map<std::string, std::vector<Entity*>>::iterator it;

   for(it = m_collisionMap.begin(); it != m_collisionMap.end(); it++)
   {
      
   }

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
void Scene::checkCollisions()
{
   std::map<std::string, std::vector<Entity*>>::iterator firstMapIt;
   std::map<std::string, std::vector<Entity*>>::iterator secondMapIt;

   for(firstMapIt = m_collisionMap.begin(); firstMapIt != m_collisionMap.end(); firstMapIt++)
   {
      for (secondMapIt = m_collisionMap.begin(); secondMapIt!= m_collisionMap.end(); secondMapIt++ )
      {
         for (int i = 0; i < firstMapIt->second.size(); i++)
         {
            Entity* entity1 = firstMapIt->second[i];
            
            for (int j = 0; j < secondMapIt->second.size(); j++)
            {
               Entity* entity2 = secondMapIt->second[j];

               if ( entity1->getName() != entity2->getName() )
               {
                  Vector3 size1 = entity1->getScale();
                  Vector3 size2 = entity2->getScale();

                  Vector3 pos1 = entity1->getPosition();
                  Vector3 pos2 = entity2->getPosition();

                  float dx = abs( pos1.x - pos2.x );
                  float dy = abs( pos1.y - pos2.y );

                  
                  if ( (dx < size1.x/2 + size2.x/2) && (dy < size1.y/2 + size2.y/2) )
                  {
                     //float penetrationX = size1.x/2 + size2.x/2 - dx;
                     //float penetrationY = size1.y/2 + size2.y/2 - dy;
                     entity1->onCollision(entity2);
                     entity2->onCollision(entity1);
                  }
   
               }
            }
         }
      }
   }
}